package com.ajaxw.crawler;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import com.ajaxw.database.DBAccess;

public class JobThread extends Thread {
	private boolean isStopped = false;
	private Dispatcher dispatcher;

	public JobThread(Dispatcher disp) {
		this.dispatcher = disp;
	}

	public void terminate() {
		this.isStopped = true;
	}

	public void run() {
		DBAccess db = null;
		try {
			db = new DBAccess();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JobInfo info = null;
		Crawler c = new Crawler();
		Map<String, String> rst;
		String txt = null;
		String enc = null;
		String rsp = null;

		while (!this.isStopped) {
			info = this.dispatcher.getNext();
			try {
				rst = c.getPage(info.getUrl());
				if (rst == null)
					continue;
				txt = rst.get("txt");
				txt = txt.replace("'", "''");
				txt = txt.replace("\\", "\\\\");
				enc = rst.get("enc");
				rsp = rst.get("rsp");

				if (txt == null)
					continue;

				String sql = String.format(
						"update webpage set text='%s' where id=%d", txt,
						info.getId());
				System.out.println(sql);
				db.execute(sql);
			} catch (IOException e) {
				System.out.println(info.getId() + ", " + info.getUrl());
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
